'use strict';

module.exports = app => {
  const { model } = app;
  const { UUID, STRING, BOOLEAN } = app.Sequelize;

  const User = model.define(
    'User',
    {
      id: {
        type: UUID,
        primaryKey: true,
      },
      user_name: {
        type: STRING(50),
        allowNull: false,
      },
      password: {
        type: STRING(50),
        allowNull: false,
      },
      enabled: {
        type: BOOLEAN,
        defaultValue: true,
      },
    },
    {
      tableName: 'sys_user',
    }
  );

  User.associate = function() {
    const { Sys: { User, UserInfo, Role, UserRole } } = model;
    User.hasOne(UserInfo, { as: 'info', foreignKey: 'user_id' });
    User.belongsToMany(Role, { as: 'roles', foreignKey: 'user_id', through: UserRole });
  };

  return User;
};
